package com.tonggedata.dataapi.controller;

import com.tonggedata.dataapi.dto.*;
import com.tonggedata.dataapi.entity.ApiAuthHistory;
import com.tonggedata.dataapi.service.ApiAuthHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
import org.apache.dolphinscheduler.api.controller.BaseController;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.dao.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import org.apache.dolphinscheduler.common.utils.ParameterUtils;

import org.apache.dolphinscheduler.api.exceptions.ApiException;

import static org.apache.dolphinscheduler.api.enums.Status.DATA_API_ERROR;

import java.util.Map;

/**
 * api授权认证历史记录表
 *
 * @author tonggedata
 * @date 2022-06-15 20:29:53
 */
@RestController
@AllArgsConstructor
@RequestMapping("/dataapi/apiauthhistorys" )
@Api(value = "api授权认证历史记录表Controller" , tags = "api授权认证历史记录表管理" , description = "api授权认证历史记录表管理相关接口" )
public class ApiAuthHistoryController extends BaseController {

    @Autowired
    private ApiAuthHistoryService apiAuthHistoryService;


    @ApiOperation(value = "queryApiAuthHistoryListPaging" , notes = "QUERY_ApiAuthHistory_LIST_PAGING_NOTES" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "searchVal" , value = "SEARCH_VAL" , dataType = "String" ),
            @ApiImplicitParam(name = "pageNo" , value = "PAGE_NO" , required = true, dataType = "Int" , example = "1" ),
            @ApiImplicitParam(name = "pageSize" , value = "PAGE_SIZE" , required = true, dataType = "Int" , example = "20" )
    })
    @GetMapping()
    @ResponseStatus(HttpStatus.OK)
    @ApiException(DATA_API_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser" )
    public Result queryApiAuthHistoryListPaging(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                                @RequestParam(value = "searchVal" , required = false) String searchVal,
                                                @RequestParam("pageNo" ) Integer pageNo,
                                                @RequestParam("pageSize" ) Integer pageSize) {
        Result result = checkPageParams(pageNo, pageSize);
        if (!result.checkResult()) {
            return result;
        }
        searchVal = ParameterUtils.handleEscapes(searchVal);
        return apiAuthHistoryService.queryApiAuthHistoryListPaging(loginUser, searchVal, pageNo, pageSize);
    }

    @ApiOperation(value = "queryApiAuthHistoryList" , notes = "QUERY_ApiAuthHistory_LIST_BY_TYPE_NOTES" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name" , value = "名称" , required = false, dataType = "String" )
    })
    @GetMapping(value = "/list" )
    @ResponseStatus(HttpStatus.OK)
    @ApiException(DATA_API_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser" )
    public Result queryApiAuthHistoryList(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                          @RequestParam(required = false) String name) {
        Map<String, Object> result = apiAuthHistoryService.queryApiAuthHistoryList(loginUser, name);
        return returnDataList(result);
    }

    @ApiOperation(value = "queryApiAuthHistory" , notes = "QUERY_ApiAuthHistory_NOTES" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value = "ApiAuthHistory_ID" , required = true, dataType = "Int" , example = "100" )

    })
    @GetMapping(value = "/{id}" )
    @ResponseStatus(HttpStatus.OK)
    @ApiException(DATA_API_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser" )
    public Result queryApiAuthHistory(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                      @PathVariable("id" ) int id) {

        Map<String, Object> result = apiAuthHistoryService.queryApiAuthHistory(id);
        return returnDataList(result);
    }

    @ApiOperation(value = "updateApiAuthHistory" , notes = "UPDATE_ApiAuthHistory_NOTES" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value = "ApiAuthHistory_ID" , required = true, dataType = "Integer" ),
            @ApiImplicitParam(name = "apiAuthHistoryParam" , value = "ApiAuthHistory_PARAM" , required = true, dataType = "ApiAuthHistoryParamDto" )
    })
    @PutMapping(value = "/{id}" )
    @ResponseStatus(HttpStatus.OK)
    @ApiException(DATA_API_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser" )
    public Result updateApiAuthHistory(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                       @PathVariable(value = "id" ) Long id,
                                       @RequestBody ApiAuthHistoryParamDto apiAuthHistoryParam) {
        apiAuthHistoryParam.setId(id);
        return apiAuthHistoryService.updateApiAuthHistory(apiAuthHistoryParam.getId(), loginUser, apiAuthHistoryParam);
    }


    @ApiOperation(value = "deleteApiAuthHistory" , notes = "DELETE_ApiAuthHistory_NOTES" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" , value = "ApiAuthHistory_ID" , required = true, dataType = "Int" , example = "100" )
    })
    @DeleteMapping(value = "/{id}" )
    @ResponseStatus(HttpStatus.OK)
    @ApiException(DATA_API_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser" )
    public Result delete(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                         @PathVariable("id" ) int id) {
        return apiAuthHistoryService.delete(loginUser, id);
    }


    @ApiOperation(value = "createApiAuthHistory" , notes = "CREATE_ApiAuthHistory_NOTES" )
    @PostMapping()
    @ResponseStatus(HttpStatus.CREATED)
    @ApiException(DATA_API_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser" )
    public Result createApiAuthHistory(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                       @RequestBody ApiAuthHistoryParamDto apiAuthHistoryParam) {
        return apiAuthHistoryService.createApiAuthHistory(loginUser, apiAuthHistoryParam);
    }

}
